import java.awt.*;
import java.awt.event.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.swing.*;
import java.io.*;

/*
 * Handles the login process (encryption, assigning an ID#, create a new login)
 */
public class Login extends JPanel {

	private GUIPatient pinterface;
	
	private JLabel welcome;
	private JPanel panel;
        private JPanel center;
	private JTextField utext;
	private JTextField ptext;
	private JButton login;
	private JButton logout;
	private Member current;
	private MemberData list;
	private JTabbedPane jPane;
        private JPanel south;
	
	
	//CONSTRUCTOR FOR LOGIN PANE
	//Very messy atm. Just sets up the layout of the login pane.
	public Login(Member c, MemberData a, JTabbedPane j)
	{
                FileInputStream fileInput = null;
                ObjectInputStream inStream = null;
                
                try
                {
                    fileInput = new FileInputStream("memberlist");
                    inStream = new ObjectInputStream(fileInput);
                    Object obj = inStream.readObject();
                    
                    if(obj instanceof MemberData)
                    {
                        list = (MemberData) obj;
                    }
                    
                }
                catch(FileNotFoundException ex)
                {
                }
                catch(IOException ex)
                {}
                catch(ClassNotFoundException ex)
                {}

               
                
		jPane = j;
		current = c;
		list = a;
		welcome = new JLabel("Welcome to CareWare. Please Login.");
		utext = new JTextField("Username");
		ptext = new JTextField("Password");
		login = new JButton("Login");
		logout = new JButton("Logout");
		panel = new JPanel();
	
		//utext.setMaximumSize(new Dimension (200,100));
		//ptext.setMaximumSize(new Dimension (200,100));
		
		//panel.setPreferredSize(new Dimension(500,470));
		panel.setLayout(new BorderLayout());
		Color background = new Color(204,255,255);
		panel.setBackground(background);
                center = new JPanel();
                center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS));
                south = new JPanel();
                south.setLayout(new BoxLayout(south, BoxLayout.Y_AXIS));
		panel.add(welcome, BorderLayout.NORTH);
		
		center.add(utext);
		center.add(ptext);
		panel.add(center, BorderLayout.CENTER);
		south.add(login);
                south.add(logout);
		panel.add(south, BorderLayout.SOUTH);
                
		logout.setEnabled(false);
		
		this.add(panel);
		
		LoginListener li = new LoginListener();
		login.addActionListener(li);
		LogoutListener lo = new LogoutListener();
		logout.addActionListener(lo);
	}
	
	
	//LOGIN BUTTON LISTENER
	//grabs test from both text fiels. Verifies login. Displays error and resets on incorrect login.
	//adds tabs based on usertype.
	private class LoginListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			String username = utext.getText();
			MessageDigest tempDigest = null;
			try {
				tempDigest = MessageDigest.getInstance("SHA-256");
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	// Gets an instance using SHA256
			tempDigest.update(ptext.getText().getBytes()); 
			
			
			if(list.validate(username, tempDigest) !=null)
			{
				current = list.validate(username, tempDigest);
				
				//adds relevant panes if user is a doctor
				if(list.validate(username, tempDigest) instanceof Doctor)
				{
					GUIDoctor temp = new GUIDoctor((Doctor) current, list, jPane);
					jPane.addTab("New Patient", new GUINewPatient(current, list, temp));
					jPane.addTab("Actions", temp);
					

				}
				
				if(list.validate(username, tempDigest) instanceof Patient)
				{
					jPane.addTab("Update Health", new GUIPatient((Patient)current));
                                        jPane.addTab("Change Info", new GUIPatientInfo((Patient) current));
				}
				
				
				if(list.validate(username, tempDigest) instanceof Nurse)
				{
					GUINurse temp = new GUINurse((Nurse) current, list, jPane);
					jPane.addTab("New Patient", new GUINewPatientNurse(current, list, temp));
					jPane.addTab("Actions", temp);
				}
				
				//enables and disables buttons, updates text
				login.setEnabled(false);
				logout.setEnabled(true);
				welcome.setText("You are logged in");	
			}
			
			else
			{
				current = null;
				welcome.setText("Login incorrect");
				utext.setText("Username");
				ptext.setText("Password");
			}
		}
	}
	
	
	//LOGOUT BUTTON LISTENER
	//Resets all textfields. Sets current login to null. deletes all panes except the login pane.
	private class LogoutListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			current = null;
			welcome.setText("Welcome to CareWare. Please Login.");
			utext.setText("Username");
			ptext.setText("Password");
			login.setEnabled(true);
			logout.setEnabled(false);
			
			while(jPane.getTabCount() > 1)
                        {
                            jPane.remove(1);
                        }
		}
	}
}
